home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / M2Crypto / httpslib.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  5KB  |  173 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import string
  5. import sys
  6. import socket
  7. import urllib
  8. import base64
  9. from httplib import *
  10. from httplib import HTTPS_PORT
  11. import SSL
  12.  
  13. class HTTPSConnection(HTTPConnection):
  14.     default_port = HTTPS_PORT
  15.     
  16.     def __init__(self, host, port = None, strict = None, **ssl):
  17.         self.session = None
  18.         keys = ssl.keys()
  19.         
  20.         try:
  21.             keys.remove('key_file')
  22.         except ValueError:
  23.             pass
  24.  
  25.         
  26.         try:
  27.             keys.remove('cert_file')
  28.         except ValueError:
  29.             pass
  30.  
  31.         
  32.         try:
  33.             keys.remove('ssl_context')
  34.         except ValueError:
  35.             pass
  36.  
  37.         if keys:
  38.             raise IllegalKeywordArgument()
  39.         
  40.         
  41.         try:
  42.             self.ssl_ctx = ssl['ssl_context']
  43.         except KeyError:
  44.             self.ssl_ctx = SSL.Context('sslv23')
  45.  
  46.         HTTPConnection.__init__(self, host, port, strict)
  47.  
  48.     
  49.     def connect(self):
  50.         self.sock = SSL.Connection(self.ssl_ctx)
  51.         if self.session:
  52.             self.sock.set_session(self.session)
  53.         
  54.         self.sock.connect((self.host, self.port))
  55.  
  56.     
  57.     def close(self):
  58.         pass
  59.  
  60.     
  61.     def get_session(self):
  62.         return self.sock.get_session()
  63.  
  64.     
  65.     def set_session(self, session):
  66.         self.session = session
  67.  
  68.  
  69.  
  70. class HTTPS(HTTP):
  71.     _connection_class = HTTPSConnection
  72.     
  73.     def __init__(self, host = '', port = None, strict = None, **ssl):
  74.         HTTP.__init__(self, host, port, strict)
  75.         
  76.         try:
  77.             self.ssl_ctx = ssl['ssl_context']
  78.         except KeyError:
  79.             self.ssl_ctx = SSL.Context('sslv23')
  80.  
  81.         self._conn.ssl_ctx = self.ssl_ctx
  82.  
  83.  
  84.  
  85. class ProxyHTTPSConnection(HTTPSConnection):
  86.     _ports = {
  87.         'http': 80,
  88.         'https': 443 }
  89.     _AUTH_HEADER = 'Proxy-Authorization'
  90.     
  91.     def __init__(self, host, port = None, strict = None, username = None, password = None, **ssl):
  92.         HTTPSConnection.__init__(self, host, port, strict, **ssl)
  93.         self._username = username
  94.         self._password = password
  95.         self._proxy_auth = None
  96.  
  97.     
  98.     def putrequest(self, method, url, skip_host = 0, skip_accept_encoding = 0):
  99.         (proto, rest) = urllib.splittype(url)
  100.         if proto is None:
  101.             raise ValueError, 'unknown URL type: %s' % url
  102.         
  103.         (host, rest) = urllib.splithost(rest)
  104.         (host, port) = urllib.splitport(host)
  105.         if port is None:
  106.             
  107.             try:
  108.                 port = self._ports[proto]
  109.             except KeyError:
  110.                 raise ValueError, 'unknown protocol for: %s' % url
  111.             except:
  112.                 None<EXCEPTION MATCH>KeyError
  113.             
  114.  
  115.         None<EXCEPTION MATCH>KeyError
  116.         self._real_host = host
  117.         self._real_port = port
  118.         HTTPSConnection.putrequest(self, method, url, skip_host, skip_accept_encoding)
  119.  
  120.     
  121.     def putheader(self, header, value):
  122.         if header.lower() == self._AUTH_HEADER.lower():
  123.             self._proxy_auth = value
  124.         else:
  125.             HTTPSConnection.putheader(self, header, value)
  126.  
  127.     
  128.     def endheaders(self):
  129.         if not self._proxy_auth:
  130.             self._proxy_auth = self._encode_auth()
  131.         
  132.         HTTPSConnection.endheaders(self)
  133.  
  134.     
  135.     def connect(self):
  136.         HTTPConnection.connect(self)
  137.         self.sock.sendall(self._get_connect_msg())
  138.         response = HTTPResponse(self.sock)
  139.         response.begin()
  140.         code = response.status
  141.         if code != 200:
  142.             self.close()
  143.             raise socket.error, 'Proxy connection failed: %d' % code
  144.         
  145.         self._start_ssl()
  146.  
  147.     
  148.     def _get_connect_msg(self):
  149.         msg = 'CONNECT %s:%d HTTP/1.1\r\n' % (self._real_host, self._real_port)
  150.         if self._proxy_auth:
  151.             msg = msg + '%s: %s\r\n' % (self._AUTH_HEADER, self._proxy_auth)
  152.         
  153.         msg = msg + '\r\n'
  154.         return msg
  155.  
  156.     
  157.     def _start_ssl(self):
  158.         self.sock = SSL.Connection(self.ssl_ctx, self.sock)
  159.         self.sock.setup_ssl()
  160.         self.sock.set_connect_state()
  161.         self.sock.connect_ssl()
  162.  
  163.     
  164.     def _encode_auth(self):
  165.         if not self._username and self._password:
  166.             return None
  167.         
  168.         userpass = '%s:%s' % (self._username, self._password)
  169.         enc_userpass = base64.encodestring(userpass).replace('\n', '')
  170.         return 'Basic %s' % enc_userpass
  171.  
  172.  
  173.